package com.cheng.cms.controller;

import com.cheng.cms.domain.Repair;
import com.cheng.cms.domain.Residents;
import com.cheng.cms.service.RepairService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RepairController {

    @Autowired
    RepairService repairService;

    @GetMapping("/queryInfo/repair")
    public String repairInfo(ModelMap modelMap){
        //        设置分页规则
        PageHelper.startPage(1,10);
        //        返回所有分页信息参数为查询所有记录的信息
        PageInfo<Repair> pageInfo = new PageInfo<>(repairService.selectAllRepair());
        // 将维修信息放入页面中
        modelMap.put("repairList",pageInfo.getList());
        //  传入分页信息
        modelMap.put("page",pageInfo);
        System.out.println("查询信息=============>"+pageInfo.toString());
        return "/queryInfo/repairInfo";
//        List<Repair> repairList = repairService.selectAllRepair();
//        modelMap.put("repairList",repairList);
//        return "/queryInfo/repairInfo";
    }

    @GetMapping("/queryInfo/repair/condition")
    @ResponseBody
    public List<Repair> repairInfoByCondition(String buildingNo,String roomNo){
        Map<String,String> repairMap = new HashMap<>();

        if(!buildingNo.equals("全部")){
            repairMap.put("buildingNo",buildingNo);
        }
        if(!roomNo.equals("全部")){
            repairMap.put("roomNo",roomNo);
        }

        return repairService.selectAllRepairByCondition(repairMap);
    }

    /**
     * 分页查询维修信息
     * @param pageNum 页码
     * @return
     */
    @GetMapping("/queryInfo/repair/{pageNum}")
    @ResponseBody
    public PageInfo<Repair> repairInfoByPage(@PathVariable("pageNum") int pageNum, ModelMap modelMap){
        System.out.println("收到请求");
        //        设置分页规则
        PageHelper.startPage(pageNum,10);
        //        返回所有分页信息参数为查询所有记录的信息
        PageInfo<Repair> pageInfo = new PageInfo<>(repairService.selectAllRepair());
        //System.out.println("查询信息=============>"+pageInfo.getList().get(0));
        return pageInfo;
    }
}
